package com.yuxi.Controller.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.yuxi.Controller.InformationShowController;
import com.yuxi.Dao.InformationDao;
import com.yuxi.Dao.InformationPortDao;
import com.yuxi.Entity.Information;
import com.yuxi.Entity.InformationPort;
import com.yuxi.Entity.TaskInformationView;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Request.DateQuery;
import com.yuxi.Request.InformationQuery;
import com.yuxi.Request.Page;
import com.yuxi.Request.PageReturn;
import com.yuxi.Response.Obj;
import com.yuxi.Service.PrisonService;
import com.yuxi.Service.UserInformationService;
import com.yuxi.Service.UserQueryService;
import com.yuxi.Service.UserService;
import com.yuxi.Util.IPUtil;
import com.yuxi.excelEntity.TaskContainer;
import com.yuxi.excelEntity.TaskReseachWord;
import com.yuxi.view.UserPowerView;

@Controller
public class InformationShowControllerImpl implements InformationShowController {
	@Autowired
	private UserInformationService userInformationService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserQueryService  userQueryService;
	@Autowired
	private InformationDao informationDao;
	private Gson gson=new Gson(); 
	@Autowired
	private PrisonService prisonService;
	@Autowired
	private InformationPortDao informationPortDao;
	
	@Override
	public String getUserInformation(InformationQuery inforQuery,Page<Information> page, HttpSession session,HttpServletRequest request) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		List<UserInfo> userInfos=new ArrayList<>();
		if(!inforQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((inforQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				inforQuery.setIntputName(userInfos.get(0).getUserName());
				inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!inforQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((inforQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					inforQuery.setIntputName(userInfos.get(0).getUserName());
					inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
			
			
		}
		if(!inforQuery.getFinisPersonNum().equals("0")){
			 userInfos =prisonService.getUserListByAccountOrName((inforQuery.getFinisPersonNum()));
		 	if (userInfos==null) {
		 		Page<Information> p=new Page<Information>();
		 		return gson.toJson(p);
			}else{
				inforQuery.setFinisPerson(userInfos.get(0).getUserName());
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					inforQuery=pReturn.getInformationQuery();
					page.setCurrentPage(pReturn.getPageNum());
					pReturn.setOpen(false);
				}
			}
			
		}
		Page<Information> p=userInformationService.userGetInformationList(inforQuery,user, ip, page);
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(page.getCurrentPage());
		pageReturn.setInformationQuery(inforQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		return gson.toJson(p);
	}

	@Override
	public String firstCheckGetList(InformationQuery infoQuery,Page<Information> p,HttpServletRequest request, HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		List<UserInfo> userInfos=new ArrayList<>();
		if(!infoQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
			
			
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		UserPowerView userPower=userService.curLoginUserPowerView(session);
		return gson.toJson(userQueryService.getInformationFirstCheck(infoQuery, user, ip, p,userPower));
	}

	@Override
	public String reCheckList(InformationQuery infoQuery, Page<Information> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		List<UserInfo> userInfos=new ArrayList<>();
		if(!infoQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		Integer port=userService.curLoginUserPowerView(session).getMaxStructure().getId();
		return gson.toJson(userQueryService.getInformationReCheck(infoQuery, user, ip, p,port));
	}

	@Override
	public String checkList(InformationQuery infoQuery, Page<Information> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);	
		List<UserInfo> userInfos=new ArrayList<>();
		if(!infoQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		Integer port=userService.curLoginUserPowerView(session).getMaxStructure().getId();
		return gson.toJson(userQueryService.getInformationCheck(infoQuery, user, ip, p,port));
	}

	@Override
	public String infoResearchList(InformationQuery infoQuery, Page<Information> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		List<UserInfo> userInfos=new ArrayList<>();
		if(!infoQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		UserPowerView userPower=userService.curLoginUserPowerView(session);
		return gson.toJson(userQueryService.getInformationInfoResearch(infoQuery, user, ip, p,userPower));
	}

	@Override
	public String taskDispatchList(InformationQuery infoQuery, Page<Information> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);	
		List<UserInfo> userInfos=new ArrayList<>();
		if(!infoQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		Integer port=userService.curLoginUserPowerView(session).getMaxStructure().getId();
		return gson.toJson(userQueryService.getInformationTaskDispatch(infoQuery, user, ip, p,port));
	}

	@Override
	public String curTaskDispatchList(InformationQuery infoQuery, Page<Information> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		List<UserInfo> userInfos=new ArrayList<>();
		if(!infoQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		Integer port=userService.curLoginUserPowerView(session).getMaxStructure().getId();
		return gson.toJson(userQueryService.getInformationCurTaskDispatch(infoQuery, user, ip, p,port));
	}

	@Override
	public String taskFinshList(InformationQuery infoQuery, Page<TaskInformationView> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		List<UserInfo> userInfos=new ArrayList<>();
		if(!infoQuery.getIntputNumber().equals("0")){
			userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		if(!infoQuery.getFinisPersonNum().equals("0")){
			userInfos =prisonService.getUserListByAccountOrName((infoQuery.getFinisPersonNum()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setFinisPerson(userInfos.get(0).getUserName());
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		return gson.toJson(userQueryService.getInformationInfoFinsh(infoQuery, user, ip, p));
	}
	@Override
	public String getAllTimeOutInfoList(InformationQuery infoQuery, Page<TaskInformationView> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);	
		if(!infoQuery.getIntputNumber().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!infoQuery.getIntputName().equals("%")) {
				List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
			 	if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
			
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);
		UserPowerView userPowerView=userService.getUserPowerViewByUserId(user);
		infoQuery.setStructure(Integer.toString(userPowerView.getMaxStructure().getId()));
		return gson.toJson(userQueryService.getAllTimeOutInfoList(infoQuery, user, ip, p));
	}
	@Override
	public String taskAlreadyFinshList(InformationQuery infoQuery, Page<TaskInformationView> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);	
		if(!infoQuery.getIntputNumber().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((infoQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				infoQuery.setIntputName(userInfos.get(0).getUserName());
				infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if(!infoQuery.getIntputName().equals("%")){
				List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((infoQuery.getIntputName()));
			 	if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					infoQuery.setIntputName(userInfos.get(0).getUserName());
					infoQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					infoQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(infoQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		return gson.toJson(userQueryService.getInformationAlreadyFinsh(infoQuery, user, ip, p));
	}

	@Override
	public String taskAlreadyDoList(InformationQuery inforQuery,Page<Information> p, HttpServletRequest request,HttpSession session) {
		UserInfo u=userService.getCurLoginUser(session);
		if(!inforQuery.getIntputNumber().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				inforQuery.setIntputName(userInfos.get(0).getUserName());
			}
		}else{
			if(!inforQuery.getIntputName().equals("%")){
				List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputName()));
			 	if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					inforQuery.setIntputName(userInfos.get(0).getUserName());
					inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					inforQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(inforQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		Page<Information> rsp=informationDao.getInfomationByUserId(inforQuery, p,u.getUserId());
		return gson.toJson(rsp);
	}

	@Override
	public String commentInfoList(InformationQuery inforQuery, Page<Information> p, HttpSession session,HttpServletRequest request) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		if(!inforQuery.getIntputNumber().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				inforQuery.setIntputName(userInfos.get(0).getUserName());
			}
		}else{
			if(!inforQuery.getIntputName().equals("%")){
				List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputName()));
			 	if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					inforQuery.setIntputName(userInfos.get(0).getUserName());
					inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					inforQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(inforQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		Integer port=userService.curLoginUserPowerView(session).getMaxStructure().getId();
		return gson.toJson(userQueryService.getInformationComment(inforQuery, user, ip, p,port));
	}

	@Override
	public String evaluationList(InformationQuery inforQuery, Page<Information> p, HttpSession session,HttpServletRequest request) {
		UserInfo user=userService.getCurLoginUser(session);
		String ip=IPUtil.getIpAddr(request);
		List<UserInfo> userInfos=new ArrayList<>();
		if(!inforQuery.getIntputNumber().equals("0")){
			
			userInfos=prisonService.getUserListByAccountOrName((inforQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				inforQuery.setIntputName(userInfos.get(0).getUserName());
				inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!inforQuery.getIntputName().equals("%")) {
				userInfos=prisonService.getUserListByAccountOrName((inforQuery.getIntputName()));
				if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					inforQuery.setIntputName(userInfos.get(0).getUserName());
					inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
			
			
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					inforQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(inforQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		return gson.toJson(userQueryService.getInformationRate(inforQuery, user, ip, p));
	}

	

	@Override
	public String getInfoDispatch(InformationQuery inforQuery, Page<Information> p, HttpServletRequest request,
			HttpSession session) {
		UserInfo u=userService.getCurLoginUser(session);	
		if(!inforQuery.getIntputNumber().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				inforQuery.setIntputName(userInfos.get(0).getUserName());
				inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!inforQuery.getIntputName().equals("%")) {
				List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputName()));
			 	if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					inforQuery.setIntputName(userInfos.get(0).getUserName());
					inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		if(!inforQuery.getFinisPersonNum().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getFinisPersonNum()));
		 	if (userInfos==null) {
		 		Page<Information> rsp=new Page<Information>();
		 		return gson.toJson(rsp);
			}else{
				inforQuery.setFinisPerson(userInfos.get(0).getUserName());
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					inforQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(inforQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		return  gson.toJson(userQueryService.getInfoDispatch(inforQuery, p, u));
	}

	@Override
	public String getUserDispatchList(InformationQuery inforQuery, Page<TaskInformationView> p, HttpSession session,HttpServletRequest request) {
		UserInfo u=userService.getCurLoginUser(session);	
		if(!inforQuery.getIntputNumber().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				inforQuery.setIntputName(userInfos.get(0).getUserName());
				inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if (!inforQuery.getIntputName().equals("%")) {
				List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputName()));
			 	if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					inforQuery.setIntputName(userInfos.get(0).getUserName());
					inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		if(!inforQuery.getFinisPersonNum().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getFinisPersonNum()));
		 	if (userInfos==null) {
		 		Page<Information> rsp=new Page<Information>();
		 		return gson.toJson(rsp);
			}else{
				inforQuery.setFinisPerson(userInfos.get(0).getUserName());
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					inforQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(inforQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		return  gson.toJson(userQueryService.getUserDispatchList(inforQuery, p, u));
	}

	@Override
	public String getCanTaskDisList(InformationQuery inforQuery, Page<Information> p, HttpSession session,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPrisonAllData(DateQuery query) {	
		try{
			List<Map<String, Object>> data=informationDao.getPriosnALlData(query);
			
			List<InformationPort> portDataSource=informationPortDao.getAllInfoPort();
			data=getSortList(data,portDataSource);	
			Obj obj=new Obj(true, "获取数据成功",data, System.currentTimeMillis(), null, "获取监狱汇总数据");	
			
			return gson.toJson(obj);
		}catch(Exception e){
			e.printStackTrace();
			Obj obj=new Obj(false, "获取数据失败",null, System.currentTimeMillis(), "10001", "获取监狱汇总数据");
			return gson.toJson(obj);
		}
	}
	
	
	
	private List<Map<String, Object>> getSortList(List<Map<String, Object>> datas,List<InformationPort> pData){
		List<InformationPort> one=getOneList(pData);
		List<Map<String,Object>> rsList=new ArrayList<Map<String, Object>>();
		
		for (Iterator iterator = one.iterator(); iterator.hasNext();) {
			InformationPort informationPort = (InformationPort) iterator.next();
			List<InformationPort> cp=findChild(informationPort, pData);
			cp.add(informationPort);
			Map<String, Object> item=new HashMap<String, Object>();
			item.put("name", informationPort.getPortName());
			item.put("list",cp);
			rsList.add(item);
		}
		
		List<Map<String,Object>> sortRsList=new ArrayList<Map<String,Object>>();
		
		
		for (Iterator iterator = datas.iterator(); iterator.hasNext();) {
			Map<String,Object> cur = (Map<String,Object>) iterator.next();			
			String name=check(Integer.parseInt(cur.get("safetyPortId").toString()),rsList);	
			cur.put("safetyPortNames", name);			
			sortRsList=addOneToList(name,cur.get("name").toString(),cur,sortRsList);
		}
		
		return sortRsList;		
	}
	
	private List<Map<String,Object>> addOneToList(String name,String pname,Map<String,Object> item,List<Map<String,Object>> rs){
		boolean isDo=false;
		try{
			for(int i=0;i<rs.size();i++){
				Map<String,Object> cur=rs.get(i);
				if(cur.get("safetyPortNames").toString().equals(name)&&cur.get("name").toString().equals(pname)){			
					cur=addItemMap(item,cur);
					rs.set(i, cur);
					isDo=true;
					break;
				}
			}
			if(!isDo){			
				rs.add(item);
			}
		}catch(Exception e){
			rs.add(item);
		}
		return rs;		
	}
	
	private Map<String,Object> addItemMap(Map<String,Object> item,Map<String,Object> to){
		/*
		 * {"firstCheck":0,"curTaskDis":0,"infoCheck":0,"safetyPortNames":"人的不安全因素","safetyPortId":"20","infoReSeach":0,
		 * "infoComment":0,"infoManage":1,"infoRate":0,"reCheck":0,"taskDis":0,"infoFinsh":0,"taskFinsh":0,"name":"监狱","prisonId":"1","infoCount":1}
		 * 
		 * */
		Object sd=objAdd(to.get("firstCheck"),item.get("firstCheck"));
		
		to.put("firstCheck", sd);
		
		Object sd1=objAdd(to.get("curTaskDis"),item.get("curTaskDis"));
		to.put("curTaskDis", sd1);

		Object sd2=objAdd(to.get("infoCheck"),item.get("infoCheck"));
		to.put("infoCheck", sd2);

		Object sd3=objAdd(to.get("infoReSeach"),item.get("infoReSeach"));
		to.put("infoReSeach", sd3);
		
		Object sd4=objAdd(to.get("infoComment"),item.get("infoComment"));
		to.put("infoComment", sd4);
		
		Object sd5=objAdd(to.get("infoManage"),item.get("infoManage"));
		to.put("infoManage", sd5);
		
		Object sd6=objAdd(to.get("reCheck"),item.get("reCheck"));
		to.put("reCheck", sd6);
		
		Object sd7=objAdd(to.get("taskDis"),item.get("taskDis"));
		to.put("taskDis", sd7);
		
		Object sd8=objAdd(to.get("taskFinsh"),item.get("taskFinsh"));
		to.put("taskFinsh", sd8);
		
		Object sd9=objAdd(to.get("infoCount"),item.get("infoCount"));
		to.put("infoCount", sd9);
		
		return to;				
	}
	private Object objAdd(Object a,Object b){
		int rs=Integer.parseInt(a.toString())+Integer.parseInt(b.toString());		
		return rs;
	}
	private String check(Integer pId,List<Map<String,Object>>  rs){
		for (Iterator iterator = rs.iterator(); iterator.hasNext();) {
			Map<String, Object> map = (Map<String, Object>) iterator.next();
			List<InformationPort> data=(List<InformationPort>) map.get("list");
			if(checkIsIn(pId, data)){
				return map.get("name").toString();
			}			
		}
		return null;		
	}
	private List<InformationPort> getOneList(List<InformationPort> pData){
		
		List<InformationPort> rs= new ArrayList<InformationPort>();
		for (Iterator iterator = pData.iterator(); iterator.hasNext();) {
			InformationPort informationPort = (InformationPort) iterator.next();
			if(informationPort.getSuperId()==0){
				rs.add(informationPort);
			}			
		}		
		return rs;		
	}
	
	private  List<InformationPort> findChild(InformationPort x,List<InformationPort> source){
		
		List<InformationPort> rs=new ArrayList<InformationPort>();
		
		Integer portId=x.getId();
		Iterator<InformationPort> it=source.iterator();
		while(it.hasNext()){
			InformationPort cur=it.next();
			if(cur.getSuperId()==portId){
				rs.add(cur);
				rs.addAll(findChild(cur,source));
			}
		}
		return rs;
	}
	
	private  boolean checkIsIn(Integer itemId,List<InformationPort> data){
		
		for (Iterator iterator = data.iterator(); iterator.hasNext();) {
			InformationPort informationPort = (InformationPort) iterator.next();
			if(informationPort.getId()==itemId){
				return true;
			}
		}
		
		return false;
	}

	@Override
	public String getPrisonAllDataByMonth() {
		try{
			Obj obj=new Obj(true, "获取数据成功",informationDao.getPrisonAllDataByMonth() , System.currentTimeMillis(), null, "获取监狱汇总数据");
			return gson.toJson(obj);
		}catch(Exception e){
			Obj obj=new Obj(false, "获取数据失败",null, System.currentTimeMillis(), "10001", "获取监狱汇总数据");
			return gson.toJson(obj);
		}
	}
	@Override
	public String getAllInfoList(InformationQuery inforQuery, Page<Information> p, HttpSession session,
			HttpServletRequest request) {
		UserInfo  user=userService.getCurLoginUser(session);
		if(!inforQuery.getIntputNumber().equals("0")){
			List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputNumber()));
		 	if (userInfos==null) {
		 		Page<Information> p1=new Page<Information>();
		 		return gson.toJson(p1);
			}else{
				inforQuery.setIntputName(userInfos.get(0).getUserName());
				inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
			}
		}else{
			if(!inforQuery.getIntputName().equals("%")){
				List<UserInfo> userInfos =prisonService.getUserListByAccountOrName((inforQuery.getIntputName()));
			 	if (userInfos==null) {
			 		Page<Information> p1=new Page<Information>();
			 		return gson.toJson(p1);
				}else{
					inforQuery.setIntputName(userInfos.get(0).getUserName());
					inforQuery.setUserId(Integer.toString(userInfos.get(0).getUserId()));
				}
			}
		}
		PageReturn pReturn=(PageReturn)session.getAttribute("pageReturn");
		if (pReturn!=null) {
			if (pReturn.isOpen()==true) {
				if (pReturn.getUrl().equals(request.getServletPath())) {
					p.setCurrentPage(pReturn.getPageNum());
					inforQuery=pReturn.getInformationQuery();
					pReturn.setOpen(false);
				}
			}
			
		}
		PageReturn pageReturn=new PageReturn();
		pageReturn.setOpen(false);
		pageReturn.setPageNum(p.getCurrentPage());
		pageReturn.setInformationQuery(inforQuery);
		pageReturn.setUrl(request.getServletPath());
		session.setAttribute("pageReturn", pageReturn);	
		Integer portid=userService.curLoginUserPowerView(session).getMaxStructure().getId();
		return gson.toJson(userQueryService.getAllinfoList(inforQuery, user, p,portid));
	}
	

}
